package com.zeng.jiudian.servlet.online;

import com.alibaba.fastjson.JSONObject;
import com.zeng.jiudian.bean.OnlineModel;
import com.zeng.jiudian.bean.PageModel;
import com.zeng.jiudian.service.OnlineService;
import com.zeng.jiudian.service.impl.OnlineServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author JJ_yo
 * @Date 2020/7/30 - 17:40
 */
@WebServlet("/back/online/findAll")
public class OnlineListServlet extends HttpServlet
{
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
    {
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String beginStartDate = req.getParameter("beginStartDate");
        String endStartDate = req.getParameter("endStartDate");
        String beginEndDate = req.getParameter("beginEndDate");
        String endEndDate = req.getParameter("endEndDate");
        String roomType = req.getParameter("roomType");
        String idea = req.getParameter("idea");
        String order = req.getParameter("order");
        String lift = req.getParameter("lift");
        String pageNum = req.getParameter("pageNum");
        String pageSize = req.getParameter("pageSize");
        String nowNum = req.getParameter("nowNum");

        OnlineModel onlineModel = new OnlineModel();
        onlineModel.setName(name);
        onlineModel.setPhone(phone);
        onlineModel.setBeginStartDate(beginStartDate);
        onlineModel.setEndStartDate(endStartDate);
        onlineModel.setBeginEndDate(beginEndDate);
        onlineModel.setEndEndDate(endEndDate);
        onlineModel.setRoomtype(roomType);
        onlineModel.setIdea(idea);
        onlineModel.setOrder(order);
        onlineModel.setLift(lift);

        PageModel pageModel = new PageModel();
        pageModel.setPageNum((pageNum == null || "".equals(pageNum) || "0".equals(pageNum)) ? 1 : Integer.parseInt(pageNum));
        pageModel.setPageSize((pageSize == null || "".equals(pageSize) || "0".equals(pageSize)) ? 10 : Integer.parseInt(pageSize));
        pageModel.setNowNum((nowNum == null || "".equals(nowNum) || "0".equals(nowNum)) ? 1 : Integer.parseInt(nowNum));

        onlineModel.setPageModel(pageModel);

        OnlineService onlineService = new OnlineServiceImpl();
        Map<String, List<OnlineModel>> map = onlineService.findAll(onlineModel);
        List<OnlineModel> list = map.get("list");
        int count = onlineService.getCount(onlineModel);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        jsonObject.put("count", count);
        resp.getWriter().println(jsonObject);
    }
}
